﻿////////////////////////////////////////////////////////////////////
// © Copyright 2007 NeoGeo New Media GmbH (http://www.neogeo.com) //
////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////
// eventhandler function for a pressed key ********************
// (captures which key was pressed and handles the event)
///////////////////////////////////////////////////////////////
var CurrentKey = 0;
function handleKeys(e) 
{
    if(window.event) // in ie
        CurrentKey = window.event.keyCode;
    else if(e.which) // moth other browsers
        CurrentKey = e.which
        
    if(CurrentKey == 101 || CurrentKey == 13) // return or numpad 5
    {
        ResetAllStart();
        CurrentKey = 0;
    }
    else if(CurrentKey == 100) // numpad 4
    {
        NavigateLeft();
        CurrentKey = 0;
    }
    else if(CurrentKey == 102) // numpad 6
    {
        NavigateRight();
        CurrentKey = 0;
    }
    else if(CurrentKey == 104) // numpad 8
    {
        NavigateUp();
        CurrentKey = 0;
    }
    else if(CurrentKey == 98) // numpad 2
    {
        NavigateDown();
        CurrentKey = 0;
    }
    else if(CurrentKey == 97) // numpad 1
    {
        TranslateScene(-5,5);
        CurrentKey = 0;
    }
    else if(CurrentKey == 99) // numpad 3
    {
        TranslateScene(5,5);
        CurrentKey = 0;
    }
    else if(CurrentKey == 103) // numpad 7
    {
        TranslateScene(-5,-5);
        CurrentKey = 0;
    }
    else if(CurrentKey == 105) // numpad 9
    {
        TranslateScene(5,-5);
        CurrentKey = 0;
    }
    else if(CurrentKey == 107) // numpad +
    {
        ZoomIn();
        CurrentKey = 0;
    }
    else if(CurrentKey == 109) // numpad -
    {
        ZoomOut();
        CurrentKey = 0;
    }
    else if(CurrentKey == 37) // arrowkey left
    {
        NavigateLeft();
        CurrentKey = 0;
    }
    else if(CurrentKey == 39) // arrowkey right
    {
        NavigateRight();
        CurrentKey = 0;
    }
    else if(CurrentKey == 38) // arrowkey up
    {
        NavigateUp();
        CurrentKey = 0;
    }
    else if(CurrentKey == 40) // arrowkey down
    {
        NavigateDown();
        CurrentKey = 0;
    }   
}

///////////////////////////////////////////////////////////////
// keyup handler **********************************************
// (handles the event when the key was released)
///////////////////////////////////////////////////////////////
function handleNoKey()
{
    CurrentKey = 0;
}

///////////////////////////////////////////////////////////////
// keyboard navigation function *******************************
// (translates the scene corrosponding to the keyboard input)
///////////////////////////////////////////////////////////////
function TranslateScene(valueX,valueY)
{
    CalculateEdges();
    
    var sceneScale = MainRootElement.findName("sceneScale");
    if(ImgX <= 0 && ImgX + SizeX >= OrigSizeX && ImgY <= 0 && ImgY + SizeY >= OrigSizeY)
    {
        if(valueX == 1 || valueX == -1 || valueY == 1 || valueY == -1)
        {
            if(ImgX - valueX < 0 && ImgX + SizeX > OrigSizeX + valueX )
                sceneScale.CenterX += valueX;
            if(ImgY - (valueY-5) < 0 && ImgY + SizeY - valueY > OrigSizeY)
                sceneScale.CenterY += valueY;
        }
        else
        {
            var revertX = valueX;
            var revertY = valueY;
            if(valueX < 0) revertX = valueX * -1;
            if(valueY < 0) revertY = valueY * -1;
            
            for(var i = 0; i < revertX; i++)
            {                             
                var X = 1;
                if(valueX < 0) X = -1;
                TranslateScene(X,0);
            }
            
            for(var j = 0; j < revertY; j++)
            {
                var Y = 1;                
                if(valueY < 0) Y = -1;
                TranslateScene(0,Y);
            }
        }
    }
    else
    {                
        if(ImgX >= 0) sceneScale.CenterX = LeftSpacing + 1;
        if(ImgX + SizeX <= OrigSizeX) sceneScale.CenterX = LeftSpacing + OrigSizeX - 5;
        if(ImgY >= 0) sceneScale.CenterY = TopSpacing + 1;
        if(ImgY + SizeY <= OrigSizeY) sceneScale.CenterY = TopSpacing + OrigSizeY - 5;
    }
    //processTileLogic();
    OverviewZoom();
}